
#include <ctime>
#include <iostream>
#include "Game.h"
#include "Poulty.h"

Poulty::Poulty(Game *game, const char *pathImage, double scaleRate)
	:Object(game, pathImage,scaleRate) 
{
	setType(KindOfObject::kPoulty);
	
	setState(States::kInit);
	
}
Poulty::~Poulty()
{
	Object::~Object();
	for(int i = 0 ; i < NUMBER_OF_EGGS ; i ++)
	{
		delete arrEggs [i] ;
		arrEggs [i] = NULL;
	}
	for(int i = 0 ; i < NUMBER_OF_SHITS ; i ++)
	{
		delete arrShits [i] ;
		arrShits [i] = NULL;
	}
}
void Poulty::draw()
{
	Object::draw();
	for(int i = 0 ; i <= currentNumberofEggs ; i ++)
	{
		arrEggs [i] ->draw();
	}
	for(int i = 0 ; i <= currentNumberofShits ; i ++)
	{
		arrShits [i] ->draw();
	}
	switch (currentState)
	{
		case States::kUpdate:
			break;
		case States::kEggFallDown:
			break;
		case States::kShitFallDown:

			break;
		case States::kInit:
		
			break;
		case States::kNone:
			break;
	}
}

void Poulty::update()
{
	Object::update();
	switch (currentState)
	{
		case States::kUpdate:
			if ( ((SDL_GetTicks() - lasttime ) / 1000) >= WAITING_FOR_EGGS )
			{
				int ran = rand() % 10;
				if (ran <= 8) setState(States::kEggFallDown);
				else setState(States::kShitFallDown);
			}
			break;
		case States::kEggFallDown:
			arrEggs[currentNumberofEggs] ->setPositions( rect.sdlRect.x ,rect.sdlRect.y + rect.sdlRect.h);
			arrEggs[currentNumberofEggs] ->setState(Egg::States::kFallDown);
			arrEggs[currentNumberofEggs] ->setSpeed(game->speedForAllofEggs);
			currentNumberofEggs = (currentNumberofEggs + 1) % NUMBER_OF_EGGS ;
			lasttime = SDL_GetTicks();
			setState(States::kUpdate);
			break;
		case States::kShitFallDown:
			arrShits[currentNumberofShits] -> setPositions( rect.sdlRect.x ,rect.sdlRect.y + rect.sdlRect.h);
			arrShits[currentNumberofShits] -> setState(Egg::States::kFallDown);
			arrShits[currentNumberofShits] ->setSpeed(game->speedForAllofEggs);
			currentNumberofShits = (currentNumberofShits + 1) % NUMBER_OF_SHITS ;
			lasttime = SDL_GetTicks();
			setState(States::kUpdate);
			break;
		case States::kInit:
		{
			for(int i = 0 ; i < NUMBER_OF_EGGS ; i ++)
			{
				arrEggs [i] = new Egg(game, "Images/egg.png", 0.4);
				arrEggs [i] ->setSpeed(game->speedForAllofEggs);
			}
			for(int i = 0 ; i < NUMBER_OF_SHITS ; i ++)
			{
				arrShits[i] = new Shit(game, "Images/shit.png" , 0.3);
				arrShits[i] ->setSpeed(game->speedForAllofEggs);
			}
			rect.sdlRect.x = rand() % (SCREEN_WIDTH - rect.sdlRect.w);
			rect.sdlRect.y = game->fence.rect.sdlRect.y - rect.sdlRect.h ;

	
			currentNumberofEggs = 0;
			currentNumberofShits = 0;

			setSpeed(1);
			lasttime = SDL_GetTicks();
			lastTimeForMoving = SDL_GetTicks();
			frameRateForMoving = 60;

			int ran = rand() % 10;
			if (ran < 5) setAction(Action::kMoveLeft);
			else setAction(Action::kMoveRight);

			setState(States::kUpdate);
			break;
		}
		case States::kNone:
			break;
	}
	double delta = (double)(SDL_GetTicks() - lastTimeForMoving ) / 1000 ;
	double timeLate = 1/frameRateForMoving;
	//std::cout << rect.sdlRect.x;
	if ( delta >= timeLate )
	{
		switch (currentAction)
		{
			case Action::kMoveLeft:
			{
				if (rect.sdlRect.x >0)
					moveLeft();
				else setAction(Action::kMoveRight);
				break;
			}
			case Action::kMoveRight:
			{
				if (rect.sdlRect.x + rect.sdlRect.w < SCREEN_WIDTH)
				{
					
					moveRight();
				}
				else setAction(Action::kMoveLeft);
				break;
			}
		}

		//std::cout << delta << " " << timeLate << " " <<  lastTimeForMoving << "\n";
		lastTimeForMoving = SDL_GetTicks();

	}
	for(int i = 0 ; i < currentNumberofEggs ; i ++)
	{
		//if (arrEggs[i] ->rect.sdlRect.y + arrEggs[i] ->rect.sdlRect.h <= SCREEN_HEIGHT)
			arrEggs [i] ->update();
	}
	for(int i = 0 ; i < currentNumberofShits ; i ++)
	{
		//if (arrShits[i] ->rect.sdlRect.y + arrShits[i] ->rect.sdlRect.h <= SCREEN_HEIGHT);
			arrShits [i] ->update();
	}
}

void Poulty::updateSpeedForAllofEggs()
{
	for(int i = 0 ; i < NUMBER_OF_EGGS ; i ++)
	{
		arrEggs [i] ->setSpeed(game->speedForAllofEggs);
	}
}

void Poulty::updateSpeedForAllofShits()
{
	for(int i = 0 ; i < NUMBER_OF_SHITS ; i ++)
	{
		arrShits [i] ->setSpeed(game->speedForAllofEggs);
	}
}